class Solution {
    typedef pair<int, int> pii;
    bool check[51][51];
    int dx[4] = { 1, -1, 0, 0 };
    int dy[4] = { 0, 0, 1, -1 };
    int row, col;
    int count = 0;
public:
    int maxAreaOfIsland(vector<vector<int>>& grid)
    {
        row = grid.size(), col = grid[0].size();
        int ret = 0;
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                if (!check[i][j] && grid[i][j] == 1)
                {
                    ++count;
                    bfs(grid, i, j);
                    ret = max(ret, count);
                    count = 0;
                }
            }
        }
        return ret;
    }
    void bfs(vector<vector<int>>& grid, int i, int j)
    {
        queue<pii> q1;
        q1.push({ i, j });
        check[i][j] = true;
        while (q1.size())
        {
            auto [a, b] = q1.front();
            q1.pop();
            for (int k = 0; k < 4; k++)
            {
                int x = a + dx[k], y = b + dy[k];
                if (x >= 0 && x < row && y >= 0 && y < col && !check[x][y] && grid[x][y] == 1)
                {
                    q1.push({ x, y });
                    check[x][y] = true;
                    ++count;
                }
            }
        }
    }
};